package ebedynky.controllers;

import ebedynky.beans.*;
import ebedynky.services.ProductSpecificationService;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

/**
 * @author davidon1
 */
@Controller
public class ProductSpecificationController {

//    @InitBinder
//    protected void initBinder(WebDataBinder binder) {
//        binder.setValidator(new ProductspecificationValidator());
//    }
    /**
     * Fetches the ProductSpecification create page when requested.
     * @param model data holder for this view
     * @return string representing page location
     */
    @RequestMapping(value = "/productSpecificationAdd", method = RequestMethod.GET)
    public String getCreateForm(Model model) {
        model.addAttribute(new ProductSpecification());
        return "product/specification/createForm";
    }

    /**
     * Extracts the ProductSpecification data from the form,
     * validates them and stores them into the db.
     * @param result data binding result
     * @return string representing next (success/failure) page location
     */
    @RequestMapping(value = "/productSpecificationAdd", method = RequestMethod.POST)
    public String create(@Valid ProductSpecification productSpecification, BindingResult result) {
        if (result.hasErrors()) {
            return "product/error";
        }
        ProductSpecificationService ss = new ProductSpecificationService();
        ss.addProductSpecification(productSpecification);
        return "product/index";
    }

    /**
     * Fetches the productSpecification delete page when requested.
     * @param model data holder for this view
     * @return string representing page location
     */
    @RequestMapping(value = "/productSpecificationDel", method = RequestMethod.GET)
    public String getDeleteForm(Model model) {
        model.addAttribute(new ProductSpecification());
        return "product/specification/deleteForm";
    }

    /**
     * Deletes the corresponding ProductSpecification data from the db.
     * @param result data binding result
     * @return string representing next (success/failure) page location
     */
    @RequestMapping(value = "/productSpecificationDel", method = RequestMethod.POST)
    public String delete(@Valid ProductSpecification productSpecification, BindingResult result) {
        if (result.hasErrors()) {
            return "product/error";
        }
        ProductSpecificationService gs = new ProductSpecificationService();
        gs.deleteProductSpecification(productSpecification.getProductSpecificationID());
        return "product/index";

    }

    /**
     * Adds a specification list extracted from the db as an attribute to the model.
     * @return List<ProductSpecification> list of specifications
     */
    @ModelAttribute("specificationList")
    protected List<ProductSpecification> specificationData() {
        ProductSpecificationService ss = new ProductSpecificationService();
        List<ProductSpecification> list = ss.getAllProductSpecifications();
        return list;
    }
}
